// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 初始化主题管理器
    const themeManager = new ThemeManager();
    
    // 初始化语言管理器
    const langManager = new LanguageManager();
    
    // 将语言管理器设置为全局变量，供主题管理器使用
    window.langManager = langManager;
    
    // 主题切换器事件处理
    const themeToggle = document.querySelector('.theme-toggle');
    if (themeToggle) {
        themeToggle.addEventListener('click', function() {
            themeManager.toggleTheme();
        });
    }
    
    // 语言切换器事件处理
    const langButtons = document.querySelectorAll('.lang-btn');
    langButtons.forEach(btn => {
        btn.addEventListener('click', function() {
            const lang = this.getAttribute('data-lang');
            langManager.setLanguage(lang);
            
            // 更新按钮状态
            langButtons.forEach(b => b.classList.remove('active'));
            this.classList.add('active');
        });
    });
    
    // 设置当前语言按钮为激活状态
    const currentLang = langManager.getCurrentLanguage();
    const currentLangBtn = document.querySelector(`.lang-btn[data-lang="${currentLang}"]`);
    if (currentLangBtn) {
        currentLangBtn.classList.add('active');
    }
    
    // 导航菜单切换
    const menuToggle = document.querySelector('.menu-toggle');
    const nav = document.querySelector('nav');
    
    if (menuToggle) {
        menuToggle.addEventListener('click', function() {
            nav.classList.toggle('active');
        });
    }
    
    // 平滑滚动
    const links = document.querySelectorAll('a[href^="#"]');
    links.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            const targetId = this.getAttribute('href');
            const targetElement = document.querySelector(targetId);
            
            if (targetElement) {
                window.scrollTo({
                    top: targetElement.offsetTop - 80,
                    behavior: 'smooth'
                });
                
                // 关闭移动端菜单
                nav.classList.remove('active');
            }
        });
    });
    
    // 技能进度条动画
    const skillBars = document.querySelectorAll('.skill-progress-bar');
    const skillSection = document.querySelector('#skills');
    
    function animateSkillBars() {
        skillBars.forEach(bar => {
            const width = bar.style.width;
            bar.style.width = '0';
            setTimeout(() => {
                bar.style.width = width;
            }, 300);
        });
    }
    
    // 当滚动到技能部分时触发动画
    if (skillSection) {
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    animateSkillBars();
                    observer.disconnect(); // 只执行一次
                }
            });
        }, { threshold: 0.5 });
        
        observer.observe(skillSection);
    }
    
    // 项目卡片悬停效果增强
    const projectCards = document.querySelectorAll('.project-card');
    projectCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-10px)';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
        });
    });
    
    // 联系表单提交处理
    const contactForm = document.getElementById('contactForm');
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const name = document.getElementById('name').value;
            const email = document.getElementById('email').value;
            const message = document.getElementById('message').value;
            
            // 简单验证
            if (name && email && message) {
                // 显示成功消息
                alert('感谢您的消息！我会尽快回复您。');
                
                // 重置表单
                contactForm.reset();
            } else {
                alert('请填写所有必填字段。');
            }
        });
    }
    
    // 添加滚动动画
    const sections = document.querySelectorAll('.section');
    const observerOptions = {
        root: null,
        rootMargin: '0px',
        threshold: 0.1
    };
    
    const sectionObserver = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('animated');
            }
        });
    }, observerOptions);
    
    sections.forEach(section => {
        sectionObserver.observe(section);
    });
    
    // 添加动画类到CSS
    const style = document.createElement('style');
    style.textContent = `
        .section {
            opacity: 0;
            transform: translateY(30px);
            transition: opacity 0.6s ease-out, transform 0.6s ease-out;
        }
        
        .section.animated {
            opacity: 1;
            transform: translateY(0);
        }
    `;
    document.head.appendChild(style);
    
    // 图片加载错误处理
    const images = document.querySelectorAll('img');
    images.forEach(img => {
        img.addEventListener('error', function() {
            // 创建占位符元素
            const placeholder = document.createElement('div');
            placeholder.classList.add('project-placeholder');
            placeholder.innerHTML = '<i class="fas fa-image"></i>';
            
            // 替换图片为占位符
            if (this.parentNode) {
                this.parentNode.replaceChild(placeholder, this);
            }
        });
    });
});